<?php
/*code here*/
$db = new Database();
$auth = Auth::getAuth('current_user');
$level = $auth['UserType'];
$user_level = $db->query("SELECT * FROM tbluser_level WHERE id='$level'","row");
if($user_level['level'] == "Admin" || $user_level['level'] == "Editor"){
    
}else{
     header('Location: /home'); 
}
if(Auth::hasAuth('current_user')){
    $this->view->current_user = Auth::getAuth('current_user');
    
    $path = "images/uploaded/";
    $valid_formats = array("jpg", "png", "gif", "bmp", "JPG");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
          $name = $_FILES['file']['name'];
          $size = $_FILES['file']['size'];
          $cabinet = $_POST['fileCabinet']; // Cabinet
          $fileTitle = $_POST['fileTitle']; // File Title
          $userID = $auth['userID'];
          $dateTime = date('Y-m-d h:i:s');
          if($fileTitle==""||$cabinet==""){
               echo '
                    <div class="wrong">
                         <img src="images/warning/closed.png" width="22" height="19" class="img-notification-position">
                         <div class="notification-position">    
                            Please fill those blank fields.
                         </div>
                    </div>
               ';
          }else{
               if(strlen($name))
               {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {
                        //if($size<(1024*1024))
                        if($size<(16384*16384))
                        {
                              $actual_image_name = time().substr(str_replace(" ", "_", $txt), 0).".".$ext;
                              $tmpFile = $_FILES['file']['tmp_name'];
                              $location = $path.$actual_image_name;
                              list($width, $height) = getimagesize($tmpFile);
                              // Resize an image
                                   if($ext=="jpg" || $ext=="jpeg"){
                                        $uploadedfile = $_FILES['file']['tmp_name'];
                                        $src = imagecreatefromjpeg($uploadedfile);
                                   }
                                   else if($ext=="png"){
                                        $uploadedfile = $_FILES['file']['tmp_name'];
                                        $src = imagecreatefrompng($uploadedfile);
                                   }
                                   else{
                                        $src = imagecreatefromgif($uploadedfile);
                                   }
                                   echo $scr;
                                   
                                   list($width,$height)=getimagesize($uploadedfile);
                                   
                                   $newwidth=400;
                                   $newheight=($height/$width)*$newwidth;
                                   $tmp=imagecreatetruecolor($newwidth,$newheight);
                                   
                                   imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                                   
                                   $thumbnail = "images/uploadedThumb/".$actual_image_name;
                                   
                                   imagejpeg($tmp,$thumbnail,100);
                                   
                                   imagedestroy($src);
                                   imagedestroy($tmp);
                                   
                              
                              if(move_uploaded_file($tmpFile, $location))
                              {
                                   //   getCreated Cabinet
                                   $getCabinet = $db->query("SELECT * FROM cabinet WHERE cabinetName='$cabinet'","row");
                                   if($cabinet!=$getCabinet['cabinetName']){
                                        $cabinetLocation = "images/cabinet/".$cabinet;
                                        $inserCabinet = array("cabinetName"=>$cabinet,
                                                              "cabinetLocation"=>$cabinetLocation,
                                                              "is_active"=>1);
                                        $db->insert("cabinet",$inserCabinet);
                                        
                                        mkdir($cabinetLocation); // create a folder
                                   }
                                        //   Insert record to the database
                                        $data = array('userID'=>$userID,
                                                  'imageName'=>$fileTitle,
                                                  'fileLocation'=>$location,
                                                  'dateUploaded'=>$dateTime,
                                                  'cabinet'=>$cabinet,
                                                  'size'=>$height,
                                                  'thumbnail'=>$thumbnail);
                                        $insert = $db->insert('file',$data);
                                        
                                        // Audit Trail
                                        $insert = array("userID"=>$auth['userID'],
                                                       "actionID"=>3,
                                                       "affectedTable"=>"file",
                                                       "affectedRecord"=>$insert,
                                                       "datetime"=>date("Y-m-d  H:i:s")
                                                       );
                                         $db->insert("audit_trail",$insert);
                                   
                                   
                                   echo "<img src='images/uploaded/".$actual_image_name."'  class='preview'>";
                                   echo '<meta http-equiv="refresh" content="2;url=/upload">';
                              }
                              else
                                   echo '
                                        <div class="wrong">
                                             <img src="images/warning/closed.png" width="22" height="19" class="img-notification-position">
                                             <div class="notification-position">    
                                                  failed.
                                             </div>
                                        </div>
                                   ';
                         }
                         else
                              echo '
                                   <div class="wrong">
                                        <img src="images/warning/closed.png" width="22" height="19" class="img-notification-position">
                                        <div class="notification-position">    
                                             Image file size max 1 MB.
                                        </div>
                                   </div>
                             ';
                    }
                    else
                         echo '
                              <div class="wrong">
                                   <img src="images/warning/closed.png" width="22" height="19" class="img-notification-position">
                                   <div class="notification-position">    
                                        Invalid file format..
                                   </div>
                              </div>
                         ';
               }
               else
               echo '
                    <div class="wrong">
                        <img src="images/warning/closed.png" width="22" height="19" class="img-notification-position">
                        <div class="notification-position">    
                           Please select image..!
                        </div>
                    </div>
               ';
          }
               exit;
    }
}
else{
    header('location: /');
}
/*code here(end)*/
$this->setLayout();


 